package com.zero.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zero.yygh.cmn.client.DictFeignClient;
import com.zero.yygh.enums.DictEnum;
import com.zero.yygh.hosp.repository.HospitalRepository;
import com.zero.yygh.hosp.service.HospitalService;
import com.zero.yygh.model.hosp.Hospital;
import com.zero.yygh.vo.hosp.HospitalQueryVo;
import com.zero.yygh.vo.hosp.HospitalSetQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zero
 * @desc
 * @create 2021/3/26
 */
@Slf4j
@Service
public class HospitalServiceImpl implements HospitalService {

    @Resource
    private HospitalRepository hospitalRepository;

    @Resource
    private DictFeignClient dictFeignClient;

    /**
     * 根据医院名称模糊查询
     * @param hosname 医院名称
     * @return java.util.List<com.zero.yygh.model.hosp.Hospital>
     * @author zero
     * @date 2021/3/30 17:02
     */
    @Override
    public List<Hospital> findByHosname (String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    /**
     * 保存医院
     * @param paramMap 参数
     * @author zero
     * @date 2021/3/27 11:48
     */
    @Override
    public void save (Map<String, Object> paramMap) {
        log.info(JSONObject.toJSONString(paramMap));
        Hospital hospital = JSONObject.parseObject(JSONObject.toJSONString(paramMap),Hospital.class);
        //判断是否存在
        Hospital targetHospital = hospitalRepository.getHospitalByHoscode(hospital.getHoscode());
        if(null != targetHospital) {
            hospital.setStatus(targetHospital.getStatus());
            hospital.setCreateTime(targetHospital.getCreateTime());
        } else {
            //0：未上线 1：已上线
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
        }
        hospital.setUpdateTime(new Date());
        hospital.setIsDeleted(0);
        hospitalRepository.save(hospital);
    }

    /**
     * 根据医院编号查询医院信息
     * @param hoscode 医院编号
     * @return com.zero.yygh.model.hosp.Hospital
     * @author zero
     * @date 2021/3/27 20:37
     */
    @Override
    public Hospital getByHoscode (String hoscode) {
        return hospitalRepository.getHospitalByHoscode(hoscode);
    }

    /**
     * 分页查询医院数据
     * @param page               当前页数
     * @param limit              每页数据
     * @param hospitalQueryVo 查询参数
     * @return org.springframework.data.domain.Page<com.zero.yygh.model.hosp.Hospital>
     * @author zero
     * @date 2021/3/28 19:44
     */
    @Override
    public Page<Hospital> selectHospPage (Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {

        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        // 0 为 第一页
        Pageable pageable = PageRequest.of(page - 1, limit, sort);
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);
        // 创建匹配器，连接使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching()
                // 改变默认字符串查询方式：模糊查询
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                // 改变默认大小写忽略方式：忽略大小写
                .withIgnoreCase(true);
        // 创建查询实例
        Example<Hospital> ex = Example.of(hospital, matcher);
        Page<Hospital> pages = hospitalRepository.findAll(ex, pageable);

        pages.getContent().forEach(this::packHospital);

        return pages;
    }

    /**
     * 根据id更新状态
     * @param id     id
     * @param status 状态
     * @author zero
     * @date 2021/3/29 13:05
     */
    @Override
    public void updateStatus (String id, Integer status) {
        if(status == 0 || status == 1) {
            Hospital hospital = this.getById(id);
            hospital.setStatus(status);
            hospital.setUpdateTime(new Date());
            hospitalRepository.save(hospital);
        }
    }

    /**
     * 查询医院详情
     * @param id id
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @author zero
     * @date 2021/3/29 16:20
     */
    @Override
    public Map<String, Object> show (String id) {
        Map<String, Object> result = new HashMap<>(2);

        Hospital hospital = this.packHospital(this.getById(id));
        result.put("hospital", hospital);
        //单独处理更直观
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;

    }

    /**
     * 根据医院编号获取医院名称接口
     * @param hoscode 医院编号
     * @return java.lang.String
     * @author zero
     * @date 2021/3/30 13:34
     */
    @Override
    public String getHospName (String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if(null != hospital) {
            return hospital.getHosname();
        }
        return "";
    }

    /**
     * 医院预约挂号详情
     * @param hoscode 医院编号
     * @return Map<String, Object>
     * @author zero
     * @date 2021/4/1 15:44
     */
    @Override
    public Map<String, Object> item (String hoscode) {
        Map<String, Object> result = new HashMap<>();
        //医院详情
        Hospital hospital = this.packHospital(this.getByHoscode(hoscode));
        result.put("hospital", hospital);
        //预约规则
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;

    }

    /**
     * 医院详情信息
     * @author zero
     * @date 2021/3/29 16:25
     * @param id id
     * @return com.zero.yygh.model.hosp.Hospital
     */
    private Hospital getById(String id) {
        return hospitalRepository.findById(id).get();
    }

    /**
     * 调用查询医院相关信息
     * @author zero
     * @date 2021/3/29 10:24
     * @param hospital 处理的医院数据
     * @return com.zero.yygh.model.hosp.Hospital
     */
    private Hospital packHospital(Hospital hospital) {
        String hostypeString = dictFeignClient.getParentName(DictEnum.HOSTYPE.getDictCode(),hospital.getHostype());
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());
        hospital.getParam().put("hostypeString", hostypeString);
        hospital.getParam().put("fullAddress", provinceString + cityString + districtString + hospital.getAddress());
        return hospital;
    }


}
